<?php

namespace app\admin\model\order;

use think\Db;
use think\Model;

class OrderBill extends Model
{
    // 表名
    protected $table = 'app_ledger';
    // 设置当前模型的数据库连接
    protected $connection = 'db_zhongtai';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    /**
     * @param $type
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 分账配置
     */
    public function OrderBillData($type,$manage_id){
        $list = $this->where(['type'=>$type,'manage_id'=>$manage_id,'is_common'=>1])->where("proportion",">",0)->select();
        if($list){
            return $list;
        }else{
            return $this->where(['type'=>$type,'is_common'=>0])->where("proportion",">",0)->select();
        }
    }
    public function ladgerProportion($type,$company_type){
        return $this
            ->where("proportion",">",0)
            ->where(['type'=>$type,'company_type'=>$company_type])
            ->sum("proportion");
    }
    public function ladgerProportionMin($type,$company_type,$manage_id){
        $proportion_min = $this
            ->where(['type'=>$type,'company_type'=>$company_type,'manage_id'=>$manage_id,'is_common'=>1])
            ->sum("proportion_min");
        if($proportion_min){
            return $proportion_min;
        }else{
            $proportion_min = $this
                ->where(['type'=>$type,'company_type'=>$company_type,'is_common'=>0])
                ->sum("proportion_min");
            return $proportion_min;
        }
    }
    public function ladgerProportionMax($type,$company_type,$manage_id){
       /* return $this
            ->where(['type'=>$type,'company_type'=>$company_type])
            ->sum("proportion_max");*/
        $proportion_min = $this
            ->where(['type'=>$type,'company_type'=>$company_type,'manage_id'=>$manage_id,'is_common'=>1])
            ->sum("proportion_max");
        if($proportion_min){
            return $proportion_min;
        }else{
            $proportion_min = $this
                ->where(['type'=>$type,'company_type'=>$company_type,'is_common'=>0])
                ->sum("proportion_max");
            return $proportion_min;
        }
    }
}
